package com.jade.editor.anisotropy.parts;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.wb.swt.SWTResourceManager;

public class Grid {

	private int _dist;
	private RGB _color = new RGB(204, 204, 204);
	private Composite _layer = null;
	private boolean _drawAllow = false;
	
	
	public Grid(int distanse){
		_dist = distanse;
	}
	
	
	public Grid(int distanse, RGB color){
		_dist = distanse;
		_color = color;
	}
	
	
	public int getDistanse(){
		return _dist;
	}
	
	
	public void showGrid(boolean b){
		_drawAllow = b;
		_layer.redraw();
	}
	
	public void setColor(RGB color){
		_color = color;
		_layer.redraw();
	}
	
	public RGB getColor(){
		return _color;
	}
	
	
	public void setDistanse(int distanse){
		_dist = distanse;
		_layer.redraw();
	}
	
	
	public void addGrid(Composite parent){
		_layer = parent;
	}
	
	public void paint(PaintEvent e){
		if (_drawAllow){
			
			Color tmpFG = e.gc.getForeground();
			
			int countX = (_layer.getSize().x / _dist)+3;
			int countY = (_layer.getSize().y / _dist)+3;
			int xMax = _layer.getSize().x;
			int yMax = _layer.getSize().y;
			e.gc.setForeground(SWTResourceManager.getColor(_color));
			
			for(int i = 0, y = 0; i <= countY; i++, y+= _dist){
				e.gc.drawLine(0, y, xMax, y);
			}
			
			for(int i = 0, x = 0; i <= countX; i++, x+= _dist){
				e.gc.drawLine(x, 0, x, yMax);
			}
			
			e.gc.setForeground(tmpFG);
		}
	}
}
